package com.zyg.aspectj;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class MyAspect {

	@Pointcut("execution(* com.zyg.service.*.*(..))")
	private void myPointCut() {}
	
	@Before("myPointCut()")
	public void before() throws Throwable{
		System.out.println("前置通知");
	}
	
	@AfterReturning("myPointCut()")
	public void afterReturning() throws Throwable{
		System.out.println("后置通知");
	}
	
	@AfterThrowing(value="myPointCut()",throwing="e")
	public void throwing(Throwable e) throws Exception{
		System.out.println("异常通知："+e.getMessage());
	}
	
	@After("myPointCut()")
	public void after() throws Throwable{
		System.out.println("最终通知");
	}
	
	@Around("myPointCut()")
	public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
		System.out.println("环绕通知前");
		Object obj = proceedingJoinPoint.proceed();
		System.out.println("环绕通知后");
		return obj;
	}
	
}
